import { getTeachingResearchDetail } from '../../../api/teachingResearch'
import { getTeacherDetail } from '../../../api/user'
import { getEnroll } from '../../../api/activity'
import { setThumb } from '../../../api/comment'
import { teachingResearchCollect, getEvaluationIPage, teachingResearchEnroll } from '../../../api/teachingResearch'
import { tipOffCommit } from '../../../api/tipOff'
import { formatTime } from '../../../utils/util'

const app = getApp()
Page({
  data: {
    allowScroll: true,
    loading: true,
    activityId: '', //活动ID
    activityType: 'TEACHING_RESEARCH', //活动ID
    detailData: {},
    enrollList: {},
    teacherDetail:{},

    showReportType: false,
    reportTypes: [
      { name: '盗版侵权', value: '盗版侵权'  },
      { name: '广告或垃圾信息', value: '广告或垃圾信息'  },
      { name: '色情，淫秽，低俗信息', value: '色情，淫秽，低俗信息'  },
      { name: '反人类，反社会，反政府等信息', value: '反人类，反社会，反政府等信息'  }
    ],
    rateData:[]
  },

  onShow: function() {

  },


  onLoad: function (options) {
    this.setData({
      activityId: options.activityId
    })
    this.init(options.activityId)
  },

  init(activityId) {
    this.getTeachingResearchDetail(activityId)
    this.getEnroll(activityId)
    this.getEvaluationIPage(activityId)
  },
  // 关注触发的事件
  startScroll(){
    this.setData({ allowScroll: true })
  },
  stopScroll(){
    this.setData({ allowScroll: false })
  },
  //点赞
  async onClickThumb(){
    let _this = this;
    let params = {
      isThumb: !_this.data.detailData.thumbNum,
      subjectId: this.data.activityId,
      subjectType: _this.data.activityType
    }
    await setThumb(params)
    if(!_this.data.detailData.liked){
      this.setData({
        'detailData.thumbNum': ++this.data.detailData.thumbNum,
        'detailData.liked': !this.data.detailData.liked
      })
    }else{
      this.setData({
        'detailData.thumbNum': --this.data.detailData.thumbNum,
        'detailData.liked': !this.data.detailData.liked
      })
    }
  },
  //点击收藏
  async onClickStar(){
    let _this = this;
    let params = {
      activityId: _this.data.activityId,
      collect: !_this.data.detailData.isCollected
    }
    await teachingResearchCollect(params)

    wx.showToast({
      title: _this.data.detailData.isCollected? '取消收藏' : '收藏成功'
    })

    _this.setData({
      "detailData.isCollected" :!_this.data.detailData.isCollected
    })
  },
  //点击举报
  onClickInform(){
    this.setData({
      showReportType: true
    })
  },
  //隐藏举报列表
  cancelReportType() {
    this.setData({
      showReportType: false
    })
  },
  // 提交举报
  async confirmReportType(e){
    let params = {
      content: e.detail.name,
      targetType: 'teaching_research',
      targetId: this.data.activityId,
      targetUserId: this.data.teacherDetail.userId,
      targetName: this.data.detailData.title
    }
    await tipOffCommit(params)
  },
  //查看报名人员
  showEnrollPeople(){
    wx.navigateTo({
      url:`/packageA/pages/enrollPeopleList/enrollPeopleList?activityId=${this.data.activityId}`
    })
  },
  //获取详情数据
  async getTeachingResearchDetail(activityId){
    let res = await getTeachingResearchDetail({activityId})
    res.startTime = formatTime(res.startTime, '{y}.{m}.{d} {h}:{i}')
    res.endTime = formatTime(res.endTime, '{y}.{m}.{d} {h}:{i}')
    this.setData({
      detailData: res,
      loading: false
    })
    this.getTeacherDetail(this.data.detailData.user.userId)
  },
    // 获取教师信息
  async getTeacherDetail(userId){
    let res = await getTeacherDetail({ userId: userId })
    this.setData({
      teacherDetail: res
    })
  },
  //获取已报人员数据
  async getEnroll(activityId){
    let params = {
      condition: activityId,
      pager: {
        page: 1,
        pageSize: 5
      }
    }
    let res = await getEnroll(params)
    this.setData({
      enrollList: res
    })
  },
  //获取评价信息
  async getEvaluationIPage(activityId){
    let params = {
      condition: {
        activityId: activityId
      },
      pager: {
        page: 1,
        pageSize: 10
      }
    }
    let res = await getEvaluationIPage(params)
    this.data.rateData = res.records
  },
  //报名
  async onClickEnroll(){
    let params = {
      activityId:this.data.activityId
      // userId:this.data.detailData.user.userId
    }
    await teachingResearchEnroll(params)
    this.setData({
      'detailData.enrolled': true
    })
    wx.showToast({
      title: '报名成功'
    })
  },
  //咨询
  async onConsult(){
    wx.navigateTo({
      url: '/packageA/pages/msgRecords/msgRecords?userId=' + this.data.detailData.user.userId
    })
  }
})
